import datetime
from flask import Flask, render_template, request
from pymongo import MongoClient
app = Flask(__name__)


MONGODB_URI = "mongodb+srv://tutu:bcm123456@cluster0.cdjvphe.mongodb.net/"
client = MongoClient(MONGODB_URI)
blog_tutu = client.blog_tutu
blogs = blog_tutu.blog.find({})
contents = [ (i['content'][:20],i['date'],i['content'],) for i in blogs]

contents = [
  ['aaaaaaaa','2023-11-1','andfafasdfasdfasdfsafsdafsdafsadfasdfasd'],
  ['bbbbbb','2023-11-3','内容22222222'],

]

@app.route('/', methods=['POST','GET'])
def index():
    if request.method == 'POST':
        content = request.form.get("content")
        print(content)
        blog_tutu.blog.insert_one({
            "content":
            content,
            "date":
            datetime.date.today().strftime("%m-%d")
        })
        # contents.append(
        #     (content[:19], , content))
    blogs = blog_tutu.blog.find({})
    contents = [ (i['content'][:20],i['date'],i['content'],) for i in blogs]

    return render_template('index.html', contents=contents)
